package com.syedu.day14.homework;

import cn.hutool.core.collection.CollUtil;
import com.syedu.util.InputUtil;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;

/**
 * description：
 * @author Administrator
 * @date 2022/3/29 16:08
 */
public class CelsService {
    public static void main(String[] args) throws IOException {
        Map<Character, List<Word>> allWords = getAllWords();
        //allWords.forEach((firstLetter,listWords)-> System.out.println(firstLetter + "\n" + listWords));
        List<Word> words = allWords.get('a');
        System.out.println(words.size());
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

        //hutool的分页方法,第一个参数表示当前页(0开始),第二个参数表示每页显示多少个,第三个参数就是参与分页的集合
        int pageNo = 0;
        for (; ; ) {
            List<Word> page = CollUtil.page(pageNo++, 10, words);
            page.forEach(System.out::println);
            String str = InputUtil.getStr("是否查看下一页数据(Y/N):");
            if (!"y".equalsIgnoreCase(str)) {
                break;
            }
        }
    }

    /**
     * 查询所有的单词,并且按照首字母分类存放
     */
    public static Map<Character, List<Word>> getAllWords() throws IOException {
        Map<Character, List<Word>> map = new HashMap<>();
        //读取w.dic文件,把文件中每一行的数据当做一个字符串放入到集合中
        List<String> lines = Files.readAllLines(Path.of("src/com/syedu/day14/homework/w.dic"));
        Word word = null;
        //遍历list集合得到数据文件中每一行的内容
        for (String line : lines) {
            String[] wordPart = line.split(" ");
            List<String> list = Arrays.asList(wordPart[1].split(":"));
            word = new Word(wordPart[0], list);
            if (map.containsKey(word.getFirstLetter())) {
                map.get(word.getFirstLetter()).add(word);
            } else {
                List<Word> subList = new ArrayList<>();
                subList.add(word);
                map.put(word.getFirstLetter(), subList);
            }
        }
        return map;
    }
}